package org.behrang.macbeans.tab.bm;

import apple.laf.AquaImageFactory;
import apple.laf.CUIAquaGroupBorder;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import javax.swing.JComponent;
import javax.swing.plaf.ComponentUI;
import org.netbeans.swing.tabcontrol.TabDisplayer;
import org.netbeans.swing.tabcontrol.plaf.AbstractViewTabDisplayerUI;
import sun.java2d.SunGraphics2D;

/**
 *
 * @author Behrang Saeedzadeh <behrangsa@gmail.com>
 */
public class BMViewTabDisplayerUI extends AbstractViewTabDisplayerUI {

    public static final int TOP_PADDING = 8;
    public static final int BOTTOM_PADDING = 8;
    public static final int LEFT_PADDING = 12;
    public static final int RIGHT_PADDING = 12;
    
    public static final Color L12 = new Color(206, 206, 206);
    public static final Color L13 = new Color(182, 182, 182);
    public static final Color L14 = new Color(183, 183, 183);
    public static final Color L1 = new Color(191, 191, 191);
    
    public static final Color L21 = new Color(205, 205, 205);
    public static final Color L22 = new Color(171, 171, 171);
    public static final Color L23 = new Color(193, 193, 193);
    public static final Color L24 = new Color(210, 210, 210);
    public static final Color L2 = new Color(219, 219, 219);
    
    public static final Color L31 = new Color(177, 177, 177);
    public static final Color L32 = new Color(184, 184, 184);
    public static final Color L33 = new Color(209, 209, 209);
    
    public static final Color BG = new Color(224, 224, 224);
    
    public static final Color U1 = new Color(168, 168, 168);
    public static final Color U2 = new Color(191, 191, 191);
    public static final Color U3 = new Color(213, 213, 213);
    public static final Color U4 = new Color(223, 223, 223);
    
    private BMViewTabDisplayerUI(TabDisplayer displayer) {
        super(displayer);
        
        // prefSize = new Dimension(100, 19); //XXX huh?
    }
    
    public static ComponentUI createUI(JComponent c) {
        return new BMViewTabDisplayerUI((TabDisplayer) c);
    }

    @Override
    public Dimension getPreferredSize(JComponent c) {
        return new Dimension(100, 20);
    }
    
    
    
    @Override
    protected void paintTabBackground(Graphics g, int index, int x, int y,
                                      int width, int height) {
//        boolean first = index == 0;
//        boolean last = index == getDataModel().size() - 1;
//        int state = 0;
//        if (isActive()) {
////            
////        }
////        if (isSelected(index)) {
////            
////        }
////        if (isAttention(index)) {
////            
////        }
//        final int HONIZONTAL = 1; 	                
//        boolean selected = isSelected(index);
//        boolean hidden = false;
//        boolean active = isActive();
//        boolean leftCap = index == 0;
//        boolean rightCap = index == getDataModel().size() - 1;
//        boolean focused = isFocused(index);
//
//        // Draw the Tab
//        AquaImageFactory.
//                drawTab((SunGraphics2D) g, 
//                    x, y - 10, width, height, 
// 	            HONIZONTAL, selected, hidden, false, active, leftCap, rightCap, focused, false);
        
    }

////    @Override
//    public void paint(Graphics g, JComponent c) {        
//        CUIAquaGroupBorder.getTabbedPaneGroupBorder().paintBorder(c, g, displayer.getX(), displayer.getY(), displayer.getWidth(), displayer.getHeight());
//        super.paint(g, c);
//    }
    
    @Override
    protected void paintTabBorder(Graphics g, int index, int x, int y, int width, int height) {
        
    }

    @Override
    protected void paintTabContent(Graphics arg0, int arg1, String arg2, int arg3, int arg4, int arg5, int arg6) {
        
    }

    @Override
    public void paint(Graphics g, JComponent c) {
        Rectangle bounds = c.getBounds();        
        super.paint(g, c);
    }
    
    private void paintBackground(Graphics g, JComponent c) {
        int x = 0, y = 0;
        Rectangle bounds = c.getBounds();
        int width = bounds.width;
        int height = bounds.height - 1;
        
        g.setColor(BG);
        g.fillRect(x + 1, y + 1, width - 2, height - 2);
        
        // left
        drawPixel(g, L12, x, y + 1);
        drawPixel(g, L13, x, y + 2);
        drawPixel(g, L14, x, y + 3);
        
        g.setColor(L1);
        g.drawLine(x, y + 4, x, y + 4 + height);
        
        drawPixel(g, L21, x + 1, y);
        drawPixel(g, L22, x + 1, y + 1);
        drawPixel(g, L23, x + 1, y + 2);
        drawPixel(g, L24, x + 1, y + 3);
        
        g.setColor(L2);
        g.drawLine(x + 1, y + 4, x + 1, y + 4 + height);
        
        drawPixel(g, L31, x + 2, y);
        drawPixel(g, L32, x + 2, y + 1);
        drawPixel(g, L33, x + 2, y + 2);
        
        // right
        int _x = x;
        x = _x + width - 1;
        drawPixel(g, L12, x, y + 1);
        drawPixel(g, L13, x, y + 2);
        drawPixel(g, L14, x, y + 3);
        
        g.setColor(L1);
        g.drawLine(x, y + 4, x, y + 4 + height);
        
        x = _x + width - 3;
        drawPixel(g, L21, x + 1, y);
        drawPixel(g, L22, x + 1, y + 1);
        drawPixel(g, L23, x + 1, y + 2);
        drawPixel(g, L24, x + 1, y + 3);
        
        g.setColor(L2);
        g.drawLine(x + 1, y + 4, x + 1, y + 4 + height);
        
        x = _x + width - 5;
        drawPixel(g, L31, x + 2, y);
        drawPixel(g, L32, x + 2, y + 1);
        drawPixel(g, L33, x + 2, y + 2);
        
        x = _x;
        // up
        g.setColor(U1);
        g.drawLine(x + 3, y, x + width - 3, y);
        g.setColor(U2);
        g.drawLine(x + 3, y + 1, x + width - 3, y + 1);
        g.setColor(U3);
        g.drawLine(x + 3, y + 2, x + width - 3, y + 2);
        g.setColor(U4);
        g.drawLine(x + 3, y + 3, x + width - 3, y + 3);
    }
    
    private void drawPixel(Graphics g, Color c, int x, int y) {
        g.setColor(c);
        g.drawLine(x, y, x, y);
    }
}
